<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - postprocessing sobel</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span> Sobel</span>
			</div>

			<small>Edge Detection with a Sobel operator.</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { pass, renderOutput } from 'three/tsl';
			import { sobel } from 'three/addons/tsl/display/SobelOperatorNode.js';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
			import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
			import { Inspector } from 'three/addons/inspector/Inspector.js';

			let camera, scene, renderer, controls;
			let postProcessing;

			const params = {
				enabled: true
			};

			init();

			async function init() {

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
				camera.position.set( 0, 1, 3 );

				//

				const loader = new GLTFLoader();
				const gltf = await loader.loadAsync( 'models/gltf/DragonAttenuation.glb' );
				const model = gltf.scene.children[ 1 ];
				model.material = new THREE.MeshStandardNodeMaterial();

				scene.add( model );
			
				//

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				renderer.inspector = new Inspector();
				renderer.toneMapping = THREE.LinearToneMapping;
				document.body.appendChild( renderer.domElement );

				await renderer.init();

				const environment = new RoomEnvironment();
				const pmremGenerator = new THREE.PMREMGenerator( renderer );

				scene.environment = pmremGenerator.fromScene( environment ).texture;
				pmremGenerator.dispose();

				//

				controls = new OrbitControls( camera, renderer.domElement );
				controls.enableDamping = true;
				controls.enableZoom = false;
				controls.target.set( 0, 0.5, 0 );
				controls.update();


				// postprocessing

				postProcessing = new THREE.PostProcessing( renderer );
				postProcessing.outputColorTransform = false;

				const scenePass = pass( scene, camera );

				postProcessing.outputNode = sobel( renderOutput( scenePass ) );

				//

				const gui = renderer.inspector.createParameters( 'Settings' );
				gui.add( params, 'enabled' );

				//

				window.addEventListener( 'resize', onWindowResize );

			}


			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				controls.update();

				if ( params.enabled === true ) {

					postProcessing.render();

				} else {

					renderer.render( scene, camera );

				}

			}

		</script>
	</body>
</html>
